package com.education.platform.management.controller.course;

import com.education.platform.common.enums.DicTypeEnum;
import com.education.platform.common.exception.BusinessException;
import com.education.platform.common.util.BeanUtil;
import com.education.platform.common.util.StringUtils;
import com.education.platform.dao.entity.CourseDO;
import com.education.platform.dao.entity.CoursewareDO;
import com.education.platform.dao.entity.SysDictionaryDO;
import com.education.platform.dao.model.CourseModel;
import com.education.platform.management.enums.PushTypeEnum;
import com.education.platform.management.push.PushUtil;
import com.education.platform.management.service.CourseCommentService;
import com.education.platform.management.service.CourseService;
import com.education.platform.management.service.CoursewareService;
import com.education.platform.management.service.SysDictionaryService;
import com.education.platform.management.vo.request.CourseQueryVO;
import com.education.platform.management.vo.request.CourseUpdateVO;
import com.education.platform.management.vo.request.OnShelfVO;
import com.education.platform.management.vo.response.CourseDetailsResVO;
import com.education.platform.management.vo.response.CoursePageInfoVO;
import com.education.platform.management.vo.response.CourseResponseVO;
import com.education.platform.management.vo.response.CoursewareResponseVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* describe: 课程
* @author : tomato
* @date: 2020-06-29 10:28:07.058
**/
@RestController
@Api("课程")
@RequestMapping("course")
@Validated
public class CourseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(CourseController.class);

	@Autowired
    private CourseService courseService;

	@Autowired
    private CourseCommentService courseCommentService;
    @Autowired
    private SysDictionaryService sysDictionaryService;
    @Autowired
    private CoursewareService coursewareService;
    @Autowired
    private PushUtil pushUtil;
    /**
     * 分页查询
     * @param courseQueryVO 分页查询参数
     * @return 分页参数
     */
    @ApiOperation(value = "分页查询")
    @PostMapping("getPageList")
    public CoursePageInfoVO selectPageList(@RequestBody @Valid CourseQueryVO courseQueryVO) {

        CourseModel course = BeanUtil.convert(courseQueryVO, CourseModel.class);

        course.setDeleted(false);
        course.setGrades(this.transParam(courseQueryVO.getCourseType()));
        Page<CourseResponseVO> page = PageHelper.startPage(courseQueryVO.getPageNum(), courseQueryVO.getPageSize());
        List<CourseModel> courseList = courseService.getCourseList(course);
        CoursePageInfoVO coursePageInfo = new CoursePageInfoVO();
        BeanUtils.copyProperties(page.toPageInfo(), coursePageInfo);
        coursePageInfo.setPageNum(courseQueryVO.getPageNum());
        List<CourseResponseVO> voList = BeanUtil.convert(courseList, CourseResponseVO.class);
        coursePageInfo.setList(voList);

        return coursePageInfo;
    }

    private List<String> transParam(Integer courseType ){
        List<String> grades = null;
        if(courseType !=null){
              List<SysDictionaryDO> dictionaryDTOS = null ;
            switch (courseType){
                case 1:
                    dictionaryDTOS= sysDictionaryService.getListByDicTypeCodeList( Arrays.asList(DicTypeEnum.MIDDLE_GRADE.getValue(),
                            DicTypeEnum.HIGH_GRADE.getValue()));
                    break;
                case 2:
                    dictionaryDTOS= sysDictionaryService.getListByDicTypeCodeList( Arrays.asList(DicTypeEnum.MIDDLE_EXAM.getValue()));
                    break;
                case 3:
                    dictionaryDTOS= sysDictionaryService.getListByDicTypeCodeList( Arrays.asList(DicTypeEnum.HIGH_EXAM.getValue()));
                    break;
            }
            if(CollectionUtils.isNotEmpty(dictionaryDTOS)){
                grades = dictionaryDTOS.parallelStream().map(o->o.getCode()).collect(Collectors.toList());
            }
        }
        return grades;
    }


    /**
     * 新增课程
     * @param vo
     * @return
     */
    @ApiOperation(value = "新增/修改 课程")
    @PostMapping("save")
    public void save(@RequestBody @Valid CourseUpdateVO vo) {
        courseService.save(vo);
    }

    /**
     * 删除 课程
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除 课程")
    @DeleteMapping("delete")
    public void delete(@RequestBody List<Integer>ids) {
        if(CollectionUtils.isEmpty(ids)){
            throw new BusinessException("参数为空");
        }
        List<CourseDO> updateList = new ArrayList<>(ids.size());
        CourseDO update;
        for(Integer id:ids){
            update = new CourseDO();
            update.setId(id);
            update.setDeleted(true);
            updateList.add(update);
        }
        courseService.updateInBatch(updateList);
    }

    /**
     * 查询课程详情
     *
     * @param
     * @return
     */
    @ApiOperation(value = "查询课程详情")
    @GetMapping("getDetails")
    public CourseDetailsResVO getDetails(@RequestParam Integer courseId) {
        CourseDetailsResVO responseVO = new CourseDetailsResVO();
        CourseDO courseDTO = null;
        courseDTO = courseService.getById(courseId);
        if (courseDTO == null) {
            throw new BusinessException("课程不存在");
        }
        //课程信息
        responseVO = BeanUtil.convert(courseDTO, CourseDetailsResVO.class);
        //类型为视频
        if(courseDTO.getFileType()==1){
            CoursewareDO query =new CoursewareDO();
            query.setCourseId(courseId);
            List<CoursewareDO> details = coursewareService.getList(query);
            List<CoursewareResponseVO> coursewareResponseVOS = BeanUtil.convert(details, CoursewareResponseVO.class);
            responseVO.setWareResponseVOS(coursewareResponseVOS);
        }
        return responseVO;
    }

    @ApiOperation(value = "上架")
    @PostMapping("onShelf")
    public void onShelf(@RequestBody OnShelfVO onShelfVO) {
        List<Integer> ids = onShelfVO.getIds();
        if(CollectionUtils.isEmpty(ids)){
            throw new BusinessException("数据为空");
        }
        List<CourseDO> updateList = new ArrayList<>(ids.size());
        CourseDO courseDO;
        for(Integer id:ids){
            courseDO = new CourseDO();
            courseDO.setId(id);
            courseDO.setPublish(true);
            updateList.add(courseDO);
        }
        courseService.updateInBatch(updateList);

        //推送消息
        if(StringUtils.isNotEmpty(onShelfVO.getPushTitle())&&StringUtils.isNotEmpty(onShelfVO.getPushText())){
            for(Integer id:ids){
                pushUtil.push( PushTypeEnum.COURSE, String.valueOf(id),  onShelfVO.getPushTitle(),onShelfVO.getPushText());
            }
        }
    }
    @ApiOperation(value = "下架")
    @PostMapping("downShelf")
    public void downShelf(@RequestBody  List<Integer> ids) {
        if(CollectionUtils.isEmpty(ids)){
            throw new BusinessException("数据为空");
        }
        List<CourseDO> updateList = new ArrayList<>(ids.size());
        CourseDO courseDO;
        for(Integer id:ids){
            courseDO = new CourseDO();
            courseDO.setId(id);
            courseDO.setPublish(false);
            updateList.add(courseDO);
        }
        courseService.updateInBatch(updateList);
    }

}
